package com.raos.example;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * 全新的 httpClient使用测试
 *
 * @author: raos
 * @e-mail: 991207823@qq.com
 * @createTime: 2023-03-23 21:15
 */
public class HttpClientTest {

    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
//        test01();
        test02();
    }

    public static void test02() throws URISyntaxException, IOException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient();
        for (int i = 0; i < 10; i++) { //先不要一次性获取太多，先来10个
            //这里我们按照规律，批量获取
            HttpRequest request = HttpRequest.newBuilder().uri(
                    new URI("https://pic.netbian.com/tupian/"+(29337 + i)+".html")).build();
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//            System.out.println(response.body()); //这里打印一下看看网页

            String html = response.body();
            String prefix = "<a href=\"\" id=\"img\"><img src=\""; //先找好我们要截取的前面一段，作为前缀去匹配位置
            String suffix = "\" data-pic="; //再找好我们要截取的屁股后面紧接着的位置，作为后缀去匹配位置
            //直接定位，然后前后截取，得到最终的图片地址
            html = html.substring(html.indexOf(prefix) + prefix.length());
            html = html.substring(0, html.indexOf(suffix));
            System.out.println(html);  //最终的图片地址就有了

            //创建请求，把图片取到
            HttpRequest imageRequest = HttpRequest.newBuilder().uri(new URI("https://pic.netbian.com"+html)).build();
            //这里以输入流的方式获取，不过貌似可以直接下载文件，各位小伙伴可以单独试试看
            HttpResponse<InputStream> imageResponse = client.send(imageRequest, HttpResponse.BodyHandlers.ofInputStream());
            //拿到输入流和文件输出流
            InputStream imageInput = imageResponse.body();
            String filename = html.substring(html.lastIndexOf("/"));
            FileOutputStream stream = new FileOutputStream("D:/images/" + filename); //保存的格式
            try (stream;imageInput) { //直接把要close的变量放进来就行，简洁一些了
                int size; //下面具体保存过程的不用我多说了吧
                byte[] data = new byte[1024];
                while ((size = imageInput.read(data)) > 0) {
                    stream.write(data, 0, size);
                }
            }
        }
    }

    public static void test01() throws URISyntaxException, IOException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient(); //直接创建一个新的HttpClient
        //现在我们只需要构造一个Http请求实体，就可以让客户端帮助我们发送出去了（实际上就跟浏览器访问类似）
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("https://www.baidu.com")).build();
        //现在我们就可以把请求发送出去了，注意send方法后面还需要一个响应体处理器（内置了很多）这里我们选择ofString直接吧响应实体转换为String字符串
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        //来看看响应实体是什么吧
        System.out.println(response.body());
    }
}
